﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace VIFON.DAL
{
    public class DAPaymentType
    {
        internal List<PaymentType> GetPaymentTypes()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.PaymentTypes;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal List<PaymentType> GetPaymentList()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.PaymentTypes;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal PaymentType GetPaymentType(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    return db.PaymentTypes.FirstOrDefault(o => o.Id == id);
                }
            }
            catch
            {
                return null;
            }
        }

        internal bool Insert(PaymentType boPaymentType)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    db.PaymentTypes.InsertOnSubmit(boPaymentType);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        internal bool Update(PaymentType boPaymentType)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.PaymentTypes.FirstOrDefault(o => o.Id == boPaymentType.Id);
                    if (temp == null)
                        return false;
                    temp.Id = boPaymentType.Id;
                    temp.Name = boPaymentType.Name;                    
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        internal bool Delete(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.PaymentTypes.FirstOrDefault(o => o.Id == id);
                    if (temp == null)
                        return false;
                    db.PaymentTypes.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
